package com.java2007.hotel.controller;

import com.java2007.hotel.service.FoodTypeService;
import com.java2007.hotel.service.impl.FoodTypeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author ghy
 * @version 1.0
 * @date 2021-01-22
 */
//@WebServlet("/foodtype/save")
public class FoodTypeSaveServlet extends HttpServlet {

    private FoodTypeService foodTypeService = new FoodTypeServiceImpl();

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        try {
            String foodName = request.getParameter("foodName");
            foodTypeService.save(foodName);
            //重定向到查询接口
            response.sendRedirect(request.getContextPath() + "/foodtype/search");
        } catch (Exception e) {
            e.printStackTrace();
            response.getWriter().write("<script>alert('服务器正忙，请稍后重试');</script>");
        }
    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
